# Input:    /Data/finalData.RData
# Output:   /Paper/Figures/coefPlot_interrupted_interruptor_chamber.pdf
# Author:   JB


rm(list = ls())
require(lme4)
require(lfe)
require(tidyverse)
require(ggridges)


load('./data/finalData.RData')
dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error')) %>% select(-matches('SEVERE|AUTHOR|LIKELY')))


# Control for house versus senate (house seems much more chaotic)
summary(modSenateInted <- felm(as.formula(paste0('interrupted ~ factor(opensecretsID)',
                                              ' + poly(scale(log(nchars)),3) + poly(scale(log(tot_utterances)),3) + ',
                                              paste(paste0('topic_',1:100),collapse = ' + '),
                                              ' + ',
                                              paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                              ' + interruptor',
                                              '| docID | 0 | opensecretsID + docID')),
                            utterance_level %>% filter(all > 30,
                                                       !grepl('EXPERT',opensecretsID),
                                                       chamber == 'Senate',
                                                       ind > mind)))$coefficients %>%
  data.frame() %>% head()

toplotChamber <- summary(modSenateInted)$coefficients %>%
  data.frame() %>%
  rename_all(function(x) gsub('Estimate','est',gsub('Cluster.s.e.','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
  mutate(covs =rownames(.)) %>%
  filter(grepl('FED',covs)) %>%
  mutate(covs =  gsub('factor\\(opensecretsID\\)','',covs)) %>%
  as_tibble() %>%
  left_join(utterance_level %>% select(opensecretsID,gender,name) %>% distinct(),
            by = c('covs' = 'opensecretsID')) %>%
  mutate(stab = ifelse(grepl('FED',covs),'DC',stab),
         id = paste0(str_to_title(name),' (',gender,')'),
         fedID = ifelse(grepl('FED',covs),'Fed Chair','Not'))


summary(modHouseInted <- felm(as.formula(paste0('interrupted ~ factor(opensecretsID)',
                                             ' + poly(scale(log(nchars)),3) + poly(scale(log(tot_utterances)),3) + ',
                                             paste(paste0('topic_',1:100),collapse = ' + '),
                                             ' + ',
                                             paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                             ' + interruptor',
                                             '| docID | 0 | opensecretsID + docID')),
                           utterance_level %>% filter(all > 30,
                                                      !grepl('EXPERT',opensecretsID),
                                                      chamber == 'House',
                                                      ind > mind)))$coefficients %>%
  data.frame() %>% head()

toplotChamber <- toplotChamber %>%
  mutate(chamber = 'Senate') %>%
  bind_rows(summary(modHouseInted)$coefficients %>%
              data.frame() %>%
              rename_all(function(x) gsub('Estimate','est',gsub('Cluster.s.e.','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
              mutate(covs =rownames(.)) %>%
              filter(grepl('FED',covs)) %>%
              mutate(covs =  gsub('factor\\(opensecretsID\\)','',covs)) %>%
              as_tibble() %>%
              left_join(utterance_level %>% select(opensecretsID,gender,name) %>% distinct(),
                        by = c('covs' = 'opensecretsID')) %>%
              mutate(stab = ifelse(grepl('FED',covs),'DC',stab),
                     id = paste0(str_to_title(name),' (',gender,')'),
                     fedID = ifelse(grepl('FED',covs),'Fed Chair','Not'),
                     chamber = 'House'))




# Interruptors
summary(modSenateIntor <- felm(as.formula(paste0('interruptor ~ factor(opensecretsID)',
                                              ' + poly(scale(log(nchars)),3) + poly(scale(log(tot_utterances)),3) + ',
                                              paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                              ' + ',
                                              paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                              ' + interrupted',
                                              '| docID | 0 | opensecretsID + docID')),
                            utterance_level %>% filter(all > 30,
                                                       !grepl('EXPERT',opensecretsID),
                                                       chamber == 'Senate',
                                                       ind > mind)))$coefficients %>%
  data.frame() %>% head()

toplotChamber <- toplotChamber %>%
  mutate(outcome = 'Interrupted') %>%
  bind_rows(summary(modSenateIntor)$coefficients %>%
              data.frame() %>%
              rename_all(function(x) gsub('Estimate','est',gsub('Cluster.s.e.','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
              mutate(covs =rownames(.)) %>%
              filter(grepl('FED',covs)) %>%
              mutate(covs =  gsub('factor\\(opensecretsID\\)','',covs)) %>%
              as_tibble() %>%
              left_join(utterance_level %>% select(opensecretsID,gender,name) %>% distinct(),
                        by = c('covs' = 'opensecretsID')) %>%
              mutate(stab = ifelse(grepl('FED',covs),'DC',stab),
                     id = paste0(str_to_title(name),' (',gender,')'),
                     fedID = ifelse(grepl('FED',covs),'Fed Chair','Not')) %>%
              mutate(outcome = 'Interruptor',
                     chamber = 'Senate'))


summary(modHouseIntor <-  felm(as.formula(paste0('interruptor ~ factor(opensecretsID)',
                                              ' + poly(scale(log(nchars)),3) + poly(scale(log(tot_utterances)),3) + ',
                                              paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                              ' + ',
                                              paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                              ' + interrupted',
                                              '| docID | 0 | opensecretsID + docID')),
                            utterance_level %>% filter(all > 30,
                                                       !grepl('EXPERT',opensecretsID),
                                                       chamber == 'House',
                                                       ind > mind)))$coefficients %>%
  data.frame() %>% head()


toplotChamber <- toplotChamber %>%
  bind_rows(summary(modHouseIntor)$coefficients %>%
              data.frame() %>%
              rename_all(function(x) gsub('Estimate','est',gsub('Cluster.s.e.','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
              mutate(covs =rownames(.)) %>%
              filter(grepl('FED',covs)) %>%
              mutate(covs =  gsub('factor\\(opensecretsID\\)','',covs)) %>%
              as_tibble() %>%
              left_join(utterance_level %>% select(opensecretsID,gender,name) %>% distinct(),
                        by = c('covs' = 'opensecretsID')) %>%
              mutate(stab = ifelse(grepl('FED',covs),'DC',stab),
                     id = paste0(str_to_title(name),' (',gender,')'),
                     fedID = ifelse(grepl('FED',covs),'Fed Chair','Not')) %>%
              mutate(outcome = 'Interruptor',
                     chamber = 'House'))


pdf('./output/figures/coefPlot_interrupted_interruptor_chamber.pdf',width = 7,height = 4)
toplotChamber %>%
  mutate(id = factor(id,levels = rev(c('Alan Greenspan (M)',
                                       'Janet L. Yellen (F)',
                                       'Jerome H. Powell (M)')))) %>%
  # select(all) %>% distinct()
  ggplot(aes(x = est,y = id,color = chamber)) + 
  geom_vline(xintercept = 0,linetype = 'dashed') + 
  geom_errorbarh(aes(xmin = est-2*se,xmax = est+2*se),height = 0,position = position_dodge(width = .25),show.legend = F) + 
  geom_point(aes(shape = chamber),size =3, fill = 'white',position = position_dodge(width = .25)) + 
  theme_ridges() + 
  scale_color_manual(name = 'Chamber',values = c('black','grey50')) +
  # scale_size_continuous(name = 'Total Utterances',range = c(2,5)) + 
  scale_shape_manual(name = 'Chamber',values = c(21,22)) + 
  xlab('Fixed Effect Intercept (Reference = Bernanke)') + 
  ylab('') + 
  guides(shape = guide_legend(override.aes = list(size = 5))) + 
  facet_grid(~outcome) + 
  theme(legend.position = 'bottom',
        strip.background = element_rect(fill = 'white',color = 'black',size = .5,linetype = 'solid'))
dev.off()


# AFUCKING APSR!@!
stargazer(modHouseInted,modSenateInted,
          modHouseIntor,modSenateIntor,
          keep = 'FED|nchars|utterances|SENT|interrupt',
          add.lines = list(c('100 LDA Topic Loadings','Y','Y','Y','Y'),
                           c('Tone Probabilities','Y','Y','Y','Y'),
                           c('Hearing FE','Y','Y','Y','Y'),
                           c('Speaker FE','Y','Y','Y','Y')),
          star.char = c('\\dag','*','**','***'),star.cutoffs = c(.1,.05,.01,.001),
          keep.stat = c('n','rsq'),out = './output/tables/coefPlot_interrupted_interruptor_chamber.tex')

